#include <latop.h>

static char *mail=
"User-Agent: Microsoft-Entourage/11.1.0.040913\r\n"
//"Date: Wed, 27 Jul 2005 15:49:25 +0800\r\n"
"Subject: test\r\n"
"From: =?GB2312?B?1tzUxrfJIA==?=<zhouyunfei@xxx.com>\r\n"
"To: =?GB2312?B?1tzUxrfJIA==?=<zhouyunfei@corp.the9.com>\r\n"
//"Message-ID: <BF0D5D86.19B%zhouyunfei@corp.the9.com>\r\n"
"Mime-version: 1.0\r\n"
"Content-type: text/plain; charset=\"UTF-8\"\r\n"
//"Content-transfer-encoding: quoted-printable\r\n"
"\r\n"
"周云飞test\r\n"
".\r\n";

static char *msg;


static int _talk(SOCKET sock, char *txt)
{
    int msg_size;
    int result;

    strcpy(msg, txt);
    msg_size=strlen(msg);
    if((result=sock_write_ex(sock, (BYTE *)msg, &msg_size, 60, 0)))
        return(-100+result);
    if((result=sock_read_ex(sock, (BYTE *)msg, &msg_size, 60, 0)))
        return(-200+result);
    msg[3]=ZERO;
    return(atoi(msg));
}


int main(int argc, char *argv[])
{
    char out[2][100]={"", "corp.the9.com"};
    char *mailbox;
    struct hostent *hptr;
    int mailbox_num, mailbox_index;
    char ip[IP_SIZE+1];
    SOCKET sock;
    int msg_size;
    int errcode;
    //int i;

    /*
    if(argc != 2)
    {
        printf("argument error\n");
        return(-1);
    }


    if(parse_string(argv[1], '@', (char *)out, 2, 100) != 2)
    {
        printf("%s parse fail\n", argv[1]);
        return(-1);
    }
    */

    ALLOC_MULTI_RETERR(msg, char, SOCK_MSG_SIZE, -1);

    mailbox=out[1];

    if((hptr=gethostbyname(mailbox)) == NULL)
    {
        printf("gethostbyname %s fail\n", mailbox);
        return(-1);
    }
    for(mailbox_num=0; hptr->h_addr_list[mailbox_num]; mailbox_num++);
    if(mailbox_num == 0)
    {
        printf("no mailbox ip\n");
        return(-1);
    }
    mailbox_index=random_int(0, mailbox_num-1);
    inet_ntop(hptr->h_addrtype, hptr->h_addr_list[mailbox_index],
            ip, sizeof(ip));
    printf("mailbox ip: %s\n", ip);

    if((sock=sock_connect_ex(ip, 25, 60)) == INVALID_SOCKET)
    {
        printf("connect to %s fail\n", ip);
        return(-1);
    }
    if((errcode=sock_read_ex(sock, (BYTE *)msg, &msg_size, 60, 0)))
    {
        printf("recv connect response fail: %d, err: %d\n",
                errcode, get_last_error(NULL));
        return(-1);
    }
    msg[msg_size]=ZERO;
    if((errcode=atoi(msg)) != 220)
    {
        printf("connect response: %d\n", errcode);
        return(-1);
    }

    if((errcode=_talk(sock, "HELO\r\n")) != 250)
    {
        printf("HELO errcode: %d, err: %d\n",
                errcode, get_last_error(NULL));
        return(-1);
    }

//    for(i=0; i<2; i++)
    {
    if((errcode=_talk(sock,
                    "MAIL FROM:<zhou_arron@163.com>\r\n")) != 250)
    {
        printf("MAIL FROM errcode: %d\n", errcode);
        return(-1);
    }
    if((errcode=_talk(sock, "RCPT TO:<zhouyunfei@corp.the9.com>\r\n"))
            != 250)
    {
        printf("RCPT TO errcode: %d\n", errcode);
        return(-1);
    }
    if((errcode=_talk(sock, "DATA\r\n")) != 354)
    {
        printf("DATA errcode: %d\n", errcode);
        return(-1);
    }
    if((errcode=_talk(sock, mail)) != 250)
    {
        printf("mail errcode: %d\n", errcode);
        return(-1);
    }
    if((errcode=_talk(sock, "RSET\r\n")) != 250)
    {
        printf("RSET errcode: %d\n", errcode);
        return(-1);
    }
    }

    if((errcode=_talk(sock, "QUIT\r\n")) != 221)
    {
        printf("QUIT errcode: %d\n", errcode);
        return(-1);
    }

    sock_close(sock);

    printf("Succeed\n");

    return(0);
}
